package com.hytc.mymall.activity.login;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import com.hytc.mymall.R;
import com.hytc.mymall.application.ActivityManager;
import com.hytc.mymall.base.BaseActivity;
import com.hytc.mymall.braodcastreciver.SmsBroadCastReceiver;
import com.hytc.mymall.http.OkHttpClientManager;
import com.hytc.mymall.http.StringPopCallBack;
import com.hytc.mymall.pop.SelectPop;
import com.hytc.mymall.pop.SelectPopCallback;
import com.hytc.mymall.utils.BaseTools;
import com.hytc.mymall.utils.MD5Utils;
import com.hytc.mymall.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Arrays;
import java.util.HashMap;

public class ForgetPswActivity extends BaseActivity implements View.OnClickListener
{
    private EditText et_ensurepsw;
    private EditText et_phonenum;
    private EditText et_psw;
    private EditText et_verifycode;
    private ImageView iv_usertype;
    private SelectPop pop;
    private Spinner sp_style;
    private TextView tv_usertype;
    private TextView tv_verifycode;
    private int usertype;

    public ForgetPswActivity() {
        this.usertype = 0;
    }

    static /* synthetic */ void access$1(final ForgetPswActivity forgetPswActivity, final int usertype) {
        forgetPswActivity.usertype = usertype;
    }

    private void commit() {
        final String trim = this.et_phonenum.getText().toString().trim();
        final String trim2 = this.et_verifycode.getText().toString().trim();
        final String trim3 = this.et_psw.getText().toString().trim();
        final String trim4 = this.et_ensurepsw.getText().toString().trim();
        if (this.usertype == 0) {
            ToastUtils.showToastWithImage((Context)this, "\u8bf7\u9009\u62e9\u6ce8\u518c\u7c7b\u578b", false);
            return;
        }
        if (TextUtils.isEmpty((CharSequence)"mobilenumber")) {
            ToastUtils.showToastWithImage((Context)this, "\u8bf7\u586b\u5199\u624b\u673a\u53f7\u7801", false);
            return;
        }
        if (TextUtils.isEmpty((CharSequence)"validcode")) {
            ToastUtils.showToastWithImage((Context)this, "\u8bf7\u586b\u5199\u9a8c\u8bc1\u7801", false);
            return;
        }
        if (TextUtils.isEmpty((CharSequence)trim3) || TextUtils.isEmpty((CharSequence)trim4)) {
            ToastUtils.showToastWithImage((Context)this, "\u8bf7\u586b\u5199\u5bc6\u7801", false);
            return;
        }
        if (!trim3.equals(trim4)) {
            ToastUtils.showToastWithImage((Context)this, "\u8bf7\u4fdd\u6301\u4e24\u6b21\u5bc6\u7801\u4e00\u81f4", false);
            return;
        }
        if (!BaseTools.isPsw(trim3) || !BaseTools.isPsw(trim4)) {
            ToastUtils.showToastWithImage((Context)this, "\u8bf7\u8f93\u5165\u6b63\u786e\u7684\u5bc6\u7801", false);
            return;
        }
        final HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("mobilenumber", trim);
        hashMap.put("verifycode", trim2);
        hashMap.put("newpwd", MD5Utils.MD5(trim3));
        hashMap.put("usertype", new StringBuilder(String.valueOf(this.usertype)).toString());
        OkHttpClientManager.postAsyn("http://www.51ydgy.com/appcommon/resetLoginPwd", new StringPopCallBack(this, true) {
            @Override
            public void onResponse(final String s) {
                try {
                    final JSONObject jsonObject = new JSONObject(s);
                    final String string = jsonObject.getString("code");
                    final String string2 = jsonObject.getString("msg");
                    if (string.endsWith("0")) {
                        ToastUtils.showToastWithImage((Context)ForgetPswActivity.this, string2, true);
                        ForgetPswActivity.this.finish();
                        return;
                    }
                    ToastUtils.showToastWithImage((Context)ForgetPswActivity.this, string2, false);
                }
                catch (JSONException ex) {
                    ex.printStackTrace();
                }
            }
        }, hashMap);
    }

    private void getVerifyCode() {
        final String trim = this.et_phonenum.getText().toString().trim();
        if (!BaseTools.isMobileNO(trim)) {
            ToastUtils.showToastWithImage((Context)this, "\u8bf7\u8f93\u5165\u6b63\u786e\u7684\u624b\u673a\u53f7\u7801\uff01", false);
            return;
        }
        if (this.usertype != 0) {
            BaseTools.getVerifyCode((Context)this, this.tv_verifycode, trim, new StringBuilder(String.valueOf(ActivityManager.VERIFY_FORGET_PSW)).toString(), this.usertype);
            return;
        }
        ToastUtils.showToastWithImage((Context)this, "\u8bf7\u9009\u62e9\u8d26\u6237\u7c7b\u578b", false);
    }

    private void initSelection() {
        this.pop = new SelectPop(this, Arrays.asList(this.getResources().getStringArray(R.array.forget_style)), this.iv_usertype, this.tv_usertype, true, new SelectPopCallback() {
            @Override
            public void onItemClick(final int n) {
                if (n >= 0 && n <= 2) {
                    ForgetPswActivity.access$1(ForgetPswActivity.this, n + 1);
                }
                else if (n >= 3 && n <= 5) {
                    ForgetPswActivity.access$1(ForgetPswActivity.this, n + 24);
                }
            }
        });
    }

    private void initView() {
        final RelativeLayout relativeLayout = (RelativeLayout)this.findViewById(R.id.rl_back);
        final TextView textView = (TextView)this.findViewById(R.id.toolbar_title);
        final LinearLayout linearLayout = (LinearLayout)this.findViewById(R.id.ll_forgetpsw_usertype);
        this.tv_usertype = (TextView)this.findViewById(R.id.tv_forgetpsw_usertype);
        this.iv_usertype = (ImageView)this.findViewById(R.id.iv_forgetpsw_usertype);
        this.et_phonenum = (EditText)this.findViewById(R.id.et_forgetpsw_phonenum);
        this.et_verifycode = (EditText)this.findViewById(R.id.et_regist_verifycode);
        this.tv_verifycode = (TextView)this.findViewById(R.id.tv_forgetpsw_verifycode);
        this.et_psw = (EditText)this.findViewById(R.id.et_forget_psw);
        this.et_ensurepsw = (EditText)this.findViewById(R.id.et_forget_ensurepsw);
        final Button button = (Button)this.findViewById(R.id.btn_forgetpsw_next);
        relativeLayout.setVisibility(View.VISIBLE);
        textView.setText((CharSequence)"\u5fd8\u8bb0\u5bc6\u7801");
        this.tv_usertype.setText((CharSequence)"\u8bf7\u9009\u62e9\u7528\u6237\u7c7b\u578b");
        relativeLayout.setOnClickListener(this);
        linearLayout.setOnClickListener(this);
        this.tv_verifycode.setOnClickListener(this);
        button.setOnClickListener(this);
    }

    public void onClick(final View view) {
        switch (view.getId()) {
            default: {}
            case R.id.rl_back: {
                this.finish();
            }
            case R.id.ll_forgetpsw_usertype: {
                this.pop.showPop();
            }
            case R.id.tv_forgetpsw_verifycode: {
                this.getVerifyCode();
            }
            case R.id.btn_forgetpsw_next: {
                this.commit();
            }
        }
    }

    @Override
    protected void onCreate(final Bundle bundle) {
        super.onCreate(bundle);
        this.setContentView(R.layout.activity_forgetpsw);
        this.initView();
        this.initSelection();
        final SmsBroadCastReceiver smsBroadCastReceiver = new SmsBroadCastReceiver();
        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
        this.registerReceiver((BroadcastReceiver)smsBroadCastReceiver, intentFilter);
        smsBroadCastReceiver.setSMSReceiveListener((SmsBroadCastReceiver.SMSReceiveListener)new SmsBroadCastReceiver.SMSReceiveListener() {
            @Override
            public void getVerifyCode(final String text) {
                ForgetPswActivity.this.et_verifycode.setText((CharSequence)text);
            }
        });
    }
}
